package org.spl.elevator.view;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.spl.elevator.model.PanelDAO;

public class ButtonsExternalPanel extends JPanel implements ActionListener{
	private ButtonEl buttons[];
	private boolean upPressed = false;
	private boolean downPressed = false;
	
	public boolean isUpPressed() {
		return upPressed;
	}

	public void setUpPressed(boolean upPressed) {
		this.upPressed = upPressed;
	}

	public boolean isDownPressed() {
		return downPressed;
	}

	public void setDownPressed(boolean downPressed) {
		this.downPressed = downPressed;
	}

	public ButtonsExternalPanel(PanelDAO panel){			
		this.buttons = new ButtonEl[panel.getNumButtons()];		
		this.setLayout(new GridBagLayout());
		String name[] = new String[]{"          ","UP"};
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		if(panel.getNumButtons() > 1){		
			name[0] = "Down";
			buttons[1] = new ButtonEl(name[1]);
			buttons[1].setBackground(java.awt.Color.GRAY);
			buttons[1].addActionListener(this);
			buttons[1].setColoringButton(panel.isColoringButton());
			c.fill = GridBagConstraints.HORIZONTAL;
			c.insets = new Insets(2,2,2,2);
			c.ipadx = 50;
			c.weightx = 0.5;
			c.gridx = 0;		
			c.gridy = 0;
			this.add(buttons[1], c);
		}
		
		buttons[0] = new ButtonEl(name[0]);
		buttons[0].setBackground(java.awt.Color.GRAY);
		buttons[0].addActionListener(this);
		buttons[0].setColoringButton(panel.isColoringButton());
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(2,2,2,2);
		c.ipadx = 50;
		c.weightx = 0.5;
		c.gridx = 0;		
		c.gridy = 1;
		this.add(buttons[0], c);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().substring(0).equals("UP")){
			buttons[1].changeColor(Color.LIGHT_GRAY);
			downPressed = true;
		} else {
			buttons[0].changeColor(Color.LIGHT_GRAY);
			downPressed = true;
		}
	}
}
